
var VM = new Vue({
    el:"#roleApp",
    created:function(){
        this.lay();
        this.getNotiveLists();

    },
    data:{
        totalNum:'',
        roleLists:[],
        delMultiFlag:[],
        allChoseFlag:false,
        keyWord:'',
        keyWord2:''
    },
    filters:{
        statusFilter:function(value){
            if(value){
                return "发布"
            }else{
                return "待定"
            }
        }
    },
    methods:{
        lay:function(){

            var _this = this;
            layui.use(['laydate','element','laypage','layer','form'], function(){
                var form = layui.form();
                $ = layui.jquery;//jquery
                laydate = layui.laydate;//日期插件
                lement = layui.element();//面包导航
                laypage = layui.laypage;//分页
                layer = layui.layer;//弹出层
                window.alert = layer.alert;
                // window.confirm = layer.confirm;
                //以上模块根据需要引入
                getPageData(1);
                /*请求后台数据，分页请求*/
                function getPageData(curr) {

                    $.ajax({
                        url:"../../role/list.do?pagenum="+curr+"&pagesize="+5,
                        dataType:"json",
                        type:"POST",
                        async:false,
                        success:function (res) {
                            if(res.msg=="success"){
                                var list = res.result.dataList;
                                _this.roleLists = list;
                                console.log(list);
                                laypage.pages = res.result.total;
                                _this.totalNum = res.result.total;
                                //当只有一页时
                                if(res.result.total > 5){
                                    $(".pageShowFlag").css({"display":"none"});
                                    $(".pageHideFlag").css({"display":"block"});
                                }else if(res.result.total > 0 ){
                                    $(".pageShowFlag").css({"display":"block"});
                                    $(".pageHideFlag").css({"display":"none"});
                                }else{
                                    $(".pageShowFlag").css({"display":"none"});
                                    $(".pageHideFlag").css({"display":"none"});
                                    layer.msg("没有查询到数据！", {icon: 1});
                                }



                                laypage({
                                    cont: 'page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="pagination"></div>
                                    pages: Math.ceil(res.result.total/5), //通过后台拿到的总页数
                                    curr: curr || 1, //当前页
                                    jump: function(obj, first){ //触发分页后的回调
                                        if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr

                                            getPageData(obj.curr);

                                        }
                                    }
                                });
                            }else{
                                layer.msg("没有查询到数据！", {icon: 1})
                            }
                        },
                        error:function (XMLHttpRequest) {
                            layer.msg("系统错误", {icon: 1});
                        }
                    })
                }

                var start = {
                    min: laydate.now()
                    ,max: '2099-06-16 23:59:59'
                    ,istoday: false
                    ,choose: function(datas){
                        end.min = datas; //开始日选好后，重置结束日的最小日期
                        end.start = datas //将结束日的初始值设定为开始日
                    }
                };
                var end = {
                    min: laydate.now()
                    ,max: '2099-06-16 23:59:59'
                    ,istoday: false
                    ,choose: function(datas){
                        start.max = datas; //结束日选好后，重置开始日的最大日期
                    }
                };
                /*全选checked*/
                $("#allChecked").click(function () {
                    if ($(this).is(':checked')) {
                        $("input:checkbox").prop('checked', true)
                    } else {
                        $("input:checkbox").prop('checked', false)
                    }
                });
            });
        },
        getNotiveLists:function(){
            var _this = this;
            // this.$http.get("../../notice/list.do").then(function (res) {
            //     _this.notiveLists = res.body.result.dataList;
            //     console.log(_this.notiveLists);
            // });
        },
        allChose:function(){
            var _this = this;
            _this.allChoseFlag = !this.allChoseFlag;
            _this.delMultiFlag = [];
            this.roleLists.forEach(function(item,index){
                if(_this.allChoseFlag){_this.delMultiFlag.push(item.id);}

                if(typeof item.checked == "undefined"){
                    Vue.set(item,"checked",_this.allChoseFlag);
                }else{
                    item.checked = _this.allChoseFlag;
                }
            });
            console.log(_this.delMultiFlag);
        },
        selectSelf:function(item){
            if(typeof item.checked == "undefined"){
                Vue.set(item,"checked",true);
            }else{
                item.checked = !item.checked;
            }
        },
        move:function(item){
            var _this = this;
            $.ajax({
                type: "POST",
                url: "../../notice/updateStatus.do",
                data: {id:item.id},
                dataType: "json",
                success: function(data){
                    // alert("改变发布状态成功！");
                    item.status = !item.status;//状态的标识
                    // window.location.href = "javascript:location.replace(location.href)";
                }
            });
        },
        editDept:function(item){
            admin_edit("编辑","../../role/edit.do?pkRoleId="+ item.pkRoleId,item.pkRoleId,'800','600')
        },
        addRole:function(){
            admin_add("新增","../../role/add.do",'800','600')
        },
        delRole:function(item){
            admin_del("删除","../../role/delete.do?roleId=" + item.pkRoleId);
        },
        delMulti:function(){
            var _this = this;
            var delMultiArr = _this.delMultiFlag.join(",");
            if(!delMultiArr){
                alert("请选择删除项！");
                return false;
            }
            if(confirm("确定删除吗?")){
                console.log(delMultiArr);
                $.ajax({
                    type: "POST",
                    url: "../../notice/deleteSome.do",
                    data: {"ids":delMultiArr},
                    dataType: "json",
                    success: function(data){
                        alert("删除成功 ");
                        window.location.href = "javascript:location.replace(location.href)";
                    }
                });
            }

        },
        check:function () {
            var _this = this;
            getPageData(1);
            /*请求后台数据，分页请求*/
            function getPageData(curr) {
                _this.keyWord2 = $("#keyWord2").val();
                $.ajax({
                    url:"../../notice/list.do?pagenum="+curr+"&pagesize="+5,
                    dataType:"json",
                    data: {"title":_this.keyWord,"status":_this.keyWord2},
                    type:"POST",
                    async:false,
                    success:function (res) {
                        if(res.msg=="success"){
                            var list = res.result.dataList;
                            _this.notiveLists = list;
                            laypage.pages = res.result.total;
                            laypage({
                                cont: 'page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="pagination"></div>
                                pages: Math.ceil(res.result.total/5), //通过后台拿到的总页数
                                curr: curr || 1, //当前页
                                jump: function(obj, first){ //触发分页后的回调
                                    if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                                        getPageData(obj.curr);
                                    }
                                }
                            });
                        }else{
                            layer.msg("没有查询到数据！", {icon: 1})
                        }
                    },
                    error:function (XMLHttpRequest) {
                        layer.msg("系统错误", {icon: 1});
                    }
                })
            }
        }
    }
});//vue 实力结束

$('button.add').on("click",function(){
    //admin_add("添加公告管理","../../notice/redirect.do",'800','600')
    // layer.open({
    //     type: 2,
    //     title: '添加公告管理',
    //     maxmin: true,
    //     shadeClose: true, //点击遮罩关闭层
    //     area : ['800px' , '600px'],
    //     content: 'createNotice.html',
    //
    // });
}); //layui 代码结束



